package club.itstudio.itouc.controller

import club.itstudio.itouc.core.util.Result
import club.itstudio.itouc.core.util.ResultUtil
import club.itstudio.itouc.util.Redis
import org.springframework.web.bind.annotation.CookieValue
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 注销登录
 */

@RestController
class Logout (val redis: Redis){
    @GetMapping("/logout")
    fun logout(@CookieValue token: String?,
               request: HttpServletRequest,
               response: HttpServletResponse): Result<Any> {
        if (token != null) {
            redis.deleteJwSession(token)
        }
        request.cookies.forEach {
            if (it.name == "token") {
                it.value = null
                it.maxAge = 0
                it.path = "/"
                response.addCookie(it)
            }
        }
        return ResultUtil.success()
    }
}